package com.zzx.controller;

import com.zzx.Util.DateFormatUtil;
import com.zzx.Util.JSONUtil;
import com.zzx.bean.*;
import com.zzx.service.imp.CourierService;
import com.zzx.servlet.ResponseText;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: Spider Man
 * @Description:
 * @Date Created in 2020-09-15 16:27
 * @Modified By:
 */
public class CourierController {

    CourierService courierService = new CourierService();

    @ResponseText("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> console = courierService.console();
        Message message = new Message();
        if (console.size() == 0){
            message.setStatus(-1);
            message.setResult("没有快递员信息");
        }else {
            message.setStatus(0);
            message.setResult("快递员信息加载成功");
            message.setData(console);
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseText("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        // 1. 查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        // 2. 当前页需要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        // 3. 查询
        List<Courier> list = courierService.findAllCourierInfo(true, offset, pageNumber);
        List<Map<String, Integer>> console = courierService.console();
        List<BootstrapTableCourier> list1 = new ArrayList<>();
        for (Courier courier : list) {
            String cRegisterTime = DateFormatUtil.format(courier.getcRegisterTime());
            String cLoginTime = courier.getcLoginTime() == null?"暂未登录":DateFormatUtil.format(courier.getcLoginTime());
            BootstrapTableCourier bootstrapTableCourier = new BootstrapTableCourier(courier.getCid(),courier.getcName(),
                    courier.getcPhone(),courier.getcIdentity(),courier.getcPassword(),courier.getcDispatch(),cRegisterTime,cLoginTime);
            list1.add(bootstrapTableCourier);
        }
        Integer total = console.get(0).get("size");
        // 4. 将list封装为bootstrap-table能够识别的格式
        ResultData<BootstrapTableCourier> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        return JSONUtil.toJSON(data);
    }

    @ResponseText("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String cPhone = request.getParameter("cPhone");
        Courier courierByCPhone = courierService.findCourierByCPhone(cPhone);
        Message message = new Message();
        if (courierByCPhone == null){
            message.setStatus(-1);
            message.setResult("该手机号不存在");
        }else {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(courierByCPhone);
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseText("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        int cid = Integer.parseInt(request.getParameter("cid"));
        String cName = request.getParameter("cName");
        String cPhone = request.getParameter("cPhone");
        String cIdentity = request.getParameter("cIdentity");
        String cPassword = request.getParameter("cPassword");
        Courier courier = new Courier(cName,cPhone,cIdentity,cPassword);
        boolean flag = courierService.update(cid, courier);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("修改成功");
            message.setData(courier);
        }else {
            message.setStatus(-1);
            message.setResult("修改失败");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseText("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        Integer cid = Integer.valueOf(request.getParameter("cid"));
        boolean flag = courierService.delete(cid);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseText("/courier/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response){
        String cName = request.getParameter("cName");
        String cPhone = request.getParameter("cPhone");
        String cIdentity = request.getParameter("cIdentity");
        String cPassword = request.getParameter("cPassword");
        Courier courier = new Courier(cName,cPhone,cIdentity,cPassword);
        boolean flag = courierService.insert(courier);
        Message message = new Message();
        if (flag && !cIdentity.equals("")){
            message.setStatus(0);
            message.setResult("添加成功");
        }else {
            message.setStatus(-1);
            message.setResult("添加失败");
        }
        return JSONUtil.toJSON(message);
    }

}
